const ajax = require('../../tool/ajax');
const login = require('../login/index');
const preview = require('../component/signaturePreview');//签章预览
const affirm = require('../component/receiverAffirm');//确认收件人
const caseDetail = require('../component/caseDetail');//案件详情右侧按钮
const supplementMaterial = require('../component/supplementMaterial');//当事人被动补充材料
const courtTime = require('../component/courtTime');//当事人被动补充材料
const screen = require('../component/screen');//截图
let pupConfig, page, config, filing, court, caseInfo;

/**
 * 现场庭审脚本：再次开庭脚本暂未实现
 * @param myPupConfig 框架配置信息
 * @param myCaseInfo 案件信息,详见component/caseFile.js
 * @returns {Promise<void>}
 */
const start = async (myPupConfig, myCaseInfo) => {
  pupConfig = myPupConfig;
  page = pupConfig.page;
  config = pupConfig.config.common;
  filing = pupConfig.config.filing;
  court = pupConfig.config.court;
  caseInfo = myCaseInfo;

  //启动类型:0是上传笔录，1是全流程，-1是不启动脚本。默认为0。
  let {type = 0} = court.start.scene;
  if (type === -1) {
    return;
  } else if (type === 0) {
    await step1();
  } else if (type === 1) {
    await step1();
    await step2();
    await step3();
    await step4();
  }
}

/**
 * 步骤一：秘书上传笔录文件
 */
const step1 = async () => {
  await login(pupConfig, caseInfo.secretaryId);//秘书登录
  await openUploadRecord();
  await uploadRecord();
}

/**
 * 步骤二：申请人主动补充材料
 */
const step2 = async () => {
  await loginParty();//当事人登录
  await uploadMaterialParty();//当事人主动补充材料
  await login(pupConfig, caseInfo.secretaryId);//秘书登录
  await sendMaterialNotice();//秘书发送补已充材料通知
  await login(pupConfig, caseInfo.chiefArbitratorId);//首席仲裁员登录
  await auditMaterial();//首席仲裁员审核补充材料
}

/**
 * 步骤三：首席仲裁员发起补充材料
 */
const step3 = async () => {
  await login(pupConfig, caseInfo.chiefArbitratorId);//首席仲裁员登录
  await uploadMaterialArbitrator();//首席仲裁员发起补充材料
  await login(pupConfig, caseInfo.secretaryId);//秘书登录
  await sendMaterialNotice(1);//秘书发送补充材料通知
  await loginParty(1);//当事人登录
  await uploadMaterialPartyPassivity();//当事人被动补充材料
  await login(pupConfig, caseInfo.secretaryId);//秘书登录
  await sendMaterialNotice();//秘书发送补已充材料通知
  await login(pupConfig, caseInfo.chiefArbitratorId);//首席仲裁员登录
  await auditMaterial();//首席仲裁员审核补充材料
}

/**
 * 步骤四：首席仲裁员发起再次庭审
 */
const step4 = async () => {
  await login(pupConfig, caseInfo.chiefArbitratorId);//首席仲裁员登录
  await startCourtAgain();//首席仲裁员发起再次庭审
  await login(pupConfig, caseInfo.secretaryId);//秘书登录
  await affirmCourtAgain();//秘书确认再次开庭
  await preview(pupConfig.page);//确认签章
  await affirm(pupConfig, 2);//确认收件人
}

/**
 * 秘书上传笔录文件
 */
const uploadRecord = async () => {
  /**
   * 上传笔录文件
   */
  await page.waitFor('span.ant-upload input');
  let input = await page.$('span.ant-upload input');
  await input.uploadFile(config.imgPathUpload + 'girl.png');
  await ajax.waitForResponse(page, 'updFileStatus.do');

  await screen(page, '秘书上传笔录文件', 3);

  await page.click('.ant-modal-footer button');
  //确认跳转详情页
  await ajax.waitForResponse(pupConfig.page, 'getAllCaseInfo.do');
}

/**
 * 秘书开上传笔录文件弹框
 * @returns {Promise<boolean>} 是否执行下一步
 */
const openUploadRecord = async () => {
  //是否执行下一步
  let gonNext = true;
  let callback = async (res) => {
    //庭审状态
    let {caseStatusDescribe} = res.data;
    if (caseStatusDescribe === '庭审结束') {
      gonNext = false;
      return false;
    } else if (caseStatusDescribe === '庭审中') {
      return true;
    } else if (caseStatusDescribe === '待开庭') {
      //开庭时间
      let {openCourtTime} = res.data;
      let waitTime = new Date(openCourtTime).getTime() - new Date().getTime() + 1000 * 30;
      await page.waitFor(waitTime);
      //url后拼接'&$=%'，保证刷新页面
      await caseDetail(pupConfig, caseInfo.caseId + '&$=%');
      return false;
    }
  }
  //打开上传笔录文件弹框
  await caseDetail(pupConfig, caseInfo.caseId, 0, callback);
  return gonNext;
}

/**
 * 当事人主动补充材料
 */
const uploadMaterialParty = async () => {
  //申请人打开主动补充材料弹框
  await caseDetail(pupConfig, caseInfo.caseId, 0);

  /**
   * 填写表单
   */
  await page.waitFor('.ant-modal-body input.ant-input');
  let inputs = await page.$$('.ant-modal-body .ant-input');
  await inputs[0].type(court.materialParty.materialName);
  await inputs[1].type(court.materialParty.opinion);
  await inputs[2].type(court.materialParty.pageNumber);

  /**
   * 上传笔录文件
   */
  await page.waitFor('span.ant-upload input');
  let input = await page.$('span.ant-upload input');
  await input.uploadFile(config.imgPathUpload + 'girl.png');
  await ajax.waitForResponse(page, 'updFileStatus.do');

  await screen(page, '当事人主动补充材料', 3);

  await page.click('.ant-modal-footer button');
  //确认跳转详情页
  await ajax.waitForResponse(pupConfig.page, 'getAllCaseInfo.do');
}

/**
 * 当事方登录
 * @param type 类型，0是当事人主动补充材料，1是仲裁员发起补充材料，默认为0.
 */
const loginParty = async (type = 0) => {
  //当事方
  let recipient = type === 0 ? court.materialParty.recipient : court.materialArbitrator.recipient;
  let userId = type === 0 ? filing.proposer1.id : filing.respondent1.id;
  //当事人登录
  await login(pupConfig, userId);
}

/**
 * 秘书发送补充材料通知
 * @param type 类型，0是已补充材料通知，1是补充材料通知
 */
const sendMaterialNotice = async (type = 0) => {
  //发送补充材料通知
  await caseDetail(pupConfig, caseInfo.caseId, 0);

  if (type === 0) {
    //确认回到详情页
    await ajax.waitForResponse(page, 'getAllCaseInfo.do');
  } else {
    await affirm(pupConfig, 1);
  }
}

/**
 * 首席仲裁员审核补充材料
 */
const auditMaterial = async () => {
  //接收补充材料
  await caseDetail(pupConfig, caseInfo.caseId, 3);

  await screen(page, '审核材料', 3);

  await page.waitFor(1000);
  await page.click('.ant-modal-footer button');
  //确认跳转详情页
  await ajax.waitForResponse(pupConfig.page, 'getAllCaseInfo.do');
}

/**
 * 首席仲裁员发起补充材料
 */
const uploadMaterialArbitrator = async () => {
  //申请人打开主动补充材料弹框
  await caseDetail(pupConfig, caseInfo.caseId, 0);

  /**
   * 填写补充材料名称
   */
  await page.waitFor(1000);
  let nameLength = court.materialArbitrator.materialNames.length;
  for (let i = 0; i < nameLength; i++) {
    await page.waitFor('#materialsName' + i);
    await page.type('#materialsName' + i, court.materialArbitrator.materialNames[i]);
    if (i < nameLength - 1) {
      await page.click('#materialsAdd button');
    }
  }

  /**
   * 选择期限
   */
  await page.click('#materialsDeadline',);
  await page.waitFor(500);
  let lis = await page.$$('ul li');
  await lis[court.materialArbitrator.deadline].click();

  let checks = await page.$$('#receiver input');
  //选择接收方
  await checks[court.materialArbitrator.recipient].click();
  //填写仲裁员意见
  await page.type('#opinion', court.materialArbitrator.opinion);

  await screen(page, '首席仲裁员发起补充材料', 3);

  await page.click('.ant-modal-footer button');
  //确认跳转详情页
  await ajax.waitForResponse(pupConfig.page, 'getAllCaseInfo.do');
}

/**
 * 当事人被动补充材料
 */
const uploadMaterialPartyPassivity = async () => {
  //申请人打开被动补充材料弹框
  await caseDetail(pupConfig, caseInfo.caseId, 1);
  await supplementMaterial(pupConfig, 1);
}

/**
 * 首席仲裁员发起再次庭审
 */
const startCourtAgain = async () => {
  //首席仲裁员打开再次庭审弹框
  await caseDetail(pupConfig, caseInfo.caseId, 1);

  await page.waitFor(1000);
  await page.click('.ant-modal-footer button');
  //确认跳转详情页
  await ajax.waitForResponse(pupConfig.page, 'getAllCaseInfo.do');
}

/**
 * 秘书确认再次庭审
 */
const affirmCourtAgain = async () => {
  //秘书打开填写开庭时间弹框
  await caseDetail(pupConfig, caseInfo.caseId, 0);
  //填写开庭时间
  await courtTime(pupConfig, 1);
}

module.exports = start;